const app = getApp();
var WxParse = require('../../wxParse/html2json.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    productList: [],
    paseImg:{}
  },

  onShow:function(){
    let that = this
    that.myRequest()
  },

  myRequest:function(){
    var that = this

    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: app.globalData.URL + '/app/collection/lookCollection?openid=' + openid,

          success: function (res) {
            wx,wx.hideLoading()
            console.log(res.data)

            that.setData({
              productList: res.data.productList || [],
              paseImg: that.parseImg(res.data.productList, that)
            })
          }
        })
      },
    })
    
  },
  //解析富文本
  parseImg: function (data, that) {
    var parseImgObj = {}
    for (var i = 0; i < data.length; i++) {
      parseImgObj[data[i].id] = WxParse.html2json(data[i]['productClassPic'], 'returnData');
    }
    return parseImgObj
  },

  skipProduct:function(e){
    console.log(e.currentTarget)
    let id = e.currentTarget.dataset.name
    wx.navigateTo({
      url: '../product/product?proId='+ id,    
    })
  },

  delete_col:function(e){
    var that = this
    console.log(e.currentTarget)

    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/collection/cancelCollect?productId=' + e.currentTarget.id + '&openid=' + openid,
          success: function (res) {
            console.log(res)
            if (res.data.msg == true) {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 5000
              })
            }
            that.myRequest()
          }
        })
      },
    })
       
  },

  add_col: function (e) {
    var that = this
    console.log(e.currentTarget)

    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/product/addShopping?proId=' + e.currentTarget.id + '&num=1' + '&openid=' + openid,
          success: function (res) {
            console.log(res)
            if (res.data.msg == true) {
              wx.showToast({
                title: '加入成功',
                icon: 'success',
                duration: 3000
              })
            } else {
              wx.showToast({
                title: '加入购物车失败',
                image: '../../images/error.png'
              })
            }
          }
        })
      },
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },

  

  
})